package com.start.cloud.robotcore.properties;

import com.start.cloud.robotcore.Constants;
import com.start.cloud.robotcore.annotation.Application;
import com.start.cloud.robotcore.utils.ClassUtil;
import com.start.cloud.robotcore.utils.StringUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.util.Properties;
import java.util.Set;

/**
 * Created by xxu on 2018/4/23.
 */
public class RobotClientLocalProperties extends Properties {

    /**
     *
     */
    private static final long serialVersionUID = -827905326646091100L;

    public static final String DEFAULT_BEECONFIG_NAME = "beenv";

    private String scanPackage = "com.start";

    private String basePackage = "com.start";

    private String appName = "application";

    private String hostIndex = null;

    private String evn = "default";

    private String configServerUrl = "http://beec.inner.moredian.net:1025";

    private String configServerUsername = "user";

    private String configServerPassword;

    public void load() {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("robot.properties");
        load(inputStream);
        if (!StringUtil.isBlank(this.appName)) {
            InputStream appSelfProperties = this.getClass().getClassLoader().getResourceAsStream(this.appName + ".properties");
            if (appSelfProperties != null) {
                load(appSelfProperties);
            }
        }
    }

    public void loadFromXML(InputStream inputStream) {
        try {
            super.loadFromXML(inputStream);
        } catch (IOException e) {
        }
        doLoadDefault();
    }

    public void load(Reader reader) {
        try {
            super.load(reader);
        } catch (IOException e) {
        }
        doLoadDefault();
    }

    public void load(InputStream inputStream) {
        try {
            super.load(inputStream);
        } catch (IOException e) {
            // logger.warn("load property fail.");
        }
        doLoadDefault();
    }

    private void doLoadDefault() {

        Set<Class<?>> clazzSet = ClassUtil.scanPackageByAnnotation(this.scanPackage, true, Application.class);
        for (Class<?> clazz : clazzSet) {
            for (Annotation a : clazz.getAnnotations()) {
                if (a instanceof Application) {
                    this.appName = ((Application) a).value();
                    // 设置默认的basePackage
                    this.basePackage = clazz.getPackage().getName();
                    this.scanPackage = clazz.getPackage().getName();
                }
            }
        }
        String appNamePro = getProperty(Constants.CONFIG_APPNAME_KEY);
        String evnPro = getProperty(Constants.CONFIG_EVN_KEY);
        String configServerUrlPro = getProperty(Constants.CONFIG_SERVER_URL_KEY);
        String configServerUsernamePro = getProperty(Constants.CONFIG_SERVER_USERNAME_KEY);
        String configServerPasswordPro = getProperty(Constants.CONFIG_SERVER_PASSWORD_KEY);
        String scanPackageNamePro = getProperty(Constants.CONFIG_SCANPACKAGE_KEY);
        String hostIndex = getProperty(Constants.CONFIG_HOSTINDEX_KEY);
        if (!StringUtil.isBlank(scanPackageNamePro)) {
            this.scanPackage = scanPackageNamePro;
        }
        if (StringUtil.isBlank(this.appName)) {
            this.appName = appNamePro;
        }
        if (!StringUtil.isBlank(evnPro)) {
            this.evn = evnPro;
        }
        if (!StringUtil.isBlank(configServerUrlPro)) {
            this.configServerUrl = configServerUrlPro;
        }
        if (!StringUtil.isBlank(configServerUsernamePro)) {
            this.configServerUsername = configServerUsernamePro;
        }
        if (!StringUtil.isBlank(configServerPasswordPro)) {
            this.configServerPassword = configServerPasswordPro;
        }
        if (!StringUtil.isBlank(hostIndex)) {
            this.hostIndex = hostIndex;
        }
        if (StringUtil.isBlank(this.appName)) {
            throw new IllegalArgumentException("the application not found.");
        }
        if (StringUtil.isBlank(this.configServerUrl)) {
            throw new IllegalArgumentException("the config server not found.");
        }
        setProperty(Constants.CONFIG_APPNAME_KEY, this.appName);
    }

    public String getAppName() {
        return appName;
    }

    public String getEvn() {
        return evn;
    }

    public void setEvn(String evn) {
        this.evn = evn;
    }

    public String getConfigServerUrl() {
        return configServerUrl;
    }

    public String getConfigServerUsername() {
        return configServerUsername;
    }

    public String getConfigServerPassword() {
        return configServerPassword;
    }

    public String getScanPackage() {
        return scanPackage;
    }

    public String getBasePackage() {
        return basePackage;
    }

    public String getHostIndex() {
        return hostIndex;
    }
}
